package com.appcenter.application.mapper;

import com.appcenter.application.dto.externalapi.BusinessSystemJobDto;
import com.appcenter.application.dto.jobs.JobDto;
import com.appcenter.application.dto.jobs.ResultJobDto;
import com.appcenter.application.input.Job.GetJobsInput;
import com.appcenter.application.model.Jobs;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@org.apache.ibatis.annotations.Mapper
@Repository
public interface JobsMapper extends Mapper<Jobs> {

    @Select("<script>" +
            " select jobs.*,jobcategorys.Name JobcategoryName from  jobs JOIN  jobcategorys on jobs.JobCategoryId=jobcategorys.Id" +
            " and jobs.Deleted=0 and jobs.VersionNo=#{versionNo}" +
            // ""+
            // " <when test='AppCode =='JZ''>" +
            // " AND jobcategorys.AppCode=#{AppCode}"+
            // " </when>" +
            // ""+
            " <when test='appCode != null'>" +
            " AND jobcategorys.AppCode=#{appCode}" +
            " </when>" +
            "" +
            "" +
            " <when test='appCode==null'>" +
            " AND jobcategorys.AppCode IS NULL" +
            " </when>" +
            "" +
            " <when test='jobCategoryId!=null and idPath!=null and penetrate'>" +
            " AND jobcategorys.IdPath like concat('%',#{jobCategoryId},'%')" +
            " </when>" +
            "" +
            "" +
            " <when test='jobCategoryId!=null  and !penetrate'>" +
            " AND jobcategorys.Id=#{jobCategoryId}" +
            " </when>" +
            "" +
            "" +
            " <when test='keyword!=null'>" +
            " AND jobs.JobName like concat('%',#{keyword},'%')" +
            " </when>" +
            "" +
            " </script>")
    public List<JobDto> GetJobs(GetJobsInput AppCode);


    @Results({
            @Result(column = "{jobCode=jobCode}", property = "orgs", javaType = List.class, many = @Many
                    (select = "com.appcenter.application.mapper.OrgsMapper.getOrgsByCode")),

    })
    @Select("<script>" +
            "select j.id,j.JobCode,j.JobName from  jobs  j " +
            "inner join jobcategorys  g on j.JobCategoryId=g.Id and j.ValidStatus=1 and j.Deleted=0 " +
            "inner join apps a on   a.AppCode=g.AppCode and a.ValidStatus=1 " +
            "inner join orgjobs oj on oj.JobId=j.Id " +
            "inner join orgs o on o.Id=oj.OrgId and o.ValidStatus=1 " +
            "where j.VersionNo=#{versionNo} and oj.VersionNo=#{versionNo} and o.VersionNo=#{versionNo} " +
            "" +
            " <when test='jobCode!=null'>" +
            " and j.JobCode=#{jobCode} " +
            " </when>" +
            "" +
            " <when test='appCode!=null'>" +
            " and  a.AppCode=#{appCode}" +
            " </when>" +
            "" +
            " <when test='orgCode!=null'>" +
            " and o.OrgCode=#{orgCode} " +
            " </when>" +
            "" +
            " </script>")
    public List<ResultJobDto> GetJobsAPI(@Param("appCode") String appCode, @Param("orgCode") String orgCode, @Param("jobCode") String jobCode, @Param("versionNo") Long versionNo);

    @Results({@Result(column = "{jobCode=jobCode}", property = "orgs", javaType = List.class, many = @Many
            (select = "com.appcenter.application.mapper.OrgsMapper.getBusinessSystemOrgsByJobCode")),
    })
    @Select("<script>" +
            "select j.id,j.JobCode,j.JobName from  jobs  j " +
            "inner join jobcategorys  g on j.JobCategoryId=g.Id and j.ValidStatus=1 and j.Deleted=0 " +
            "inner join apps a on   a.AppCode=g.AppCode and a.ValidStatus=1 " +
            "inner join orgjobs oj on oj.JobId=j.Id " +
            "inner join orgs o on o.Id=oj.OrgId and o.ValidStatus=1 " +
            "where j.VersionNo=#{versionNo} and oj.VersionNo=#{versionNo} and o.VersionNo=#{versionNo} " +
            "" +
            " <when test='jobCode!=null'>" +
            " and j.JobCode=#{jobCode} " +
            " </when>" +
            "" +
            " <when test='appCode!=null'>" +
            " and  a.AppCode=#{appCode}" +
            " </when>" +
            "" +
            " <when test='orgCode!=null'>" +
            " and o.OrgCode=#{orgCode} " +
            " </when>" +
            "" +
            " </script>")
    public List<BusinessSystemJobDto> GetBusinessSystemJobs(@Param("appCode") String appCode, @Param("orgCode") String orgCode, @Param("jobCode") String jobCode, @Param("versionNo") Long versionNo);


}
